package com.example.sb.validator;

import com.example.sb.pojo.User;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * @author kedong.lin
 */
public class UserValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return aClass.equals(User.class);
    }

    @Override
    public void validate(Object target, Errors errors) {
        if(null == target){
            errors.rejectValue("",null,"用户参数不能为空");
            return;
        }
        User user = (User) target;
        if(StringUtils.isEmpty(user.getUserName())){
            errors.rejectValue("userName",null,"用户名不能为空");
        }
    }
}
